package com.ruoyi.web.controller.protocolTemplate;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.protocolTemplate.domain.ProtocolTemplate;
import com.ruoyi.protocolTemplate.service.IProtocolTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 协议模板Controller
 *
 * @author jiuyou
 * @date 2021-07-09
 */
@RestController
@RequestMapping("/systemTem/template")
public class ProtocolTemplateController extends BaseController
{
    @Autowired
    private IProtocolTemplateService protocolTemplateService;

    /**
     * 查询协议模板列表
     */
    @PreAuthorize("@ss.hasPermi('system:template:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProtocolTemplate protocolTemplate)
    {
        startPage();
        List<ProtocolTemplate> list = protocolTemplateService.selectProtocolTemplateList(protocolTemplate);
        return getDataTable(list);
    }

    /**
     * 导出协议模板列表
     */
    @PreAuthorize("@ss.hasPermi('system:template:export')")
    @Log(title = "协议模板", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ProtocolTemplate protocolTemplate)
    {
        List<ProtocolTemplate> list = protocolTemplateService.selectProtocolTemplateList(protocolTemplate);
        ExcelUtil<ProtocolTemplate> util = new ExcelUtil<ProtocolTemplate>(ProtocolTemplate.class);
        return util.exportExcel(list, "template");
    }

    /**
     * 获取协议模板详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:template:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(protocolTemplateService.selectProtocolTemplateById(id));
    }

    /**
     * 新增协议模板
     */
    @PreAuthorize("@ss.hasPermi('system:template:add')")
    @Log(title = "协议模板", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProtocolTemplate protocolTemplate)
    {
        if(null!=protocolTemplate.getTemplateText()){
            protocolTemplate.setTemplateText( protocolTemplate.getTemplateText().replaceAll("<img src=","<img style='display: block;max-width:100%;height:auto;' src="));
        }

        return toAjax(protocolTemplateService.insertProtocolTemplate(protocolTemplate));
    }

    /**
     * 修改协议模板
     */
    @PreAuthorize("@ss.hasPermi('system:template:edit')")
    @Log(title = "协议模板", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProtocolTemplate protocolTemplate)
    {
        if(null!=protocolTemplate.getTemplateText()){
            protocolTemplate.setTemplateText( protocolTemplate.getTemplateText().replaceAll("<img src=","<img style='display: block;max-width:100%;height:auto;' src="));
        }

        return toAjax(protocolTemplateService.updateProtocolTemplate(protocolTemplate));
    }

    /**
     * 删除协议模板
     */
    @PreAuthorize("@ss.hasPermi('system:template:remove')")
    @Log(title = "协议模板", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(protocolTemplateService.deleteProtocolTemplateByIds(ids));
    }
}
